﻿using System;

namespace SharpOSCAR.Base
{
    /// <summary>
    /// Represents a U16 (unsigned 16-bit integer) data type
    /// </summary>
    public class U16 : ProtocolData
    {
        private ushort _Value;
        /// <summary>
        /// Value
        /// </summary>
        public ushort Value
        {
            get { return _Value; }
            set { _Value = value; }
        }

        /// <summary>
        /// Initializes the U16 with the specified value
        /// </summary>
        /// <param name="value">Value</param>
        public U16(ushort value)
        {
            _Value = value;
        }

        /// <summary>
        /// Converts the U16 into a byte array
        /// </summary>
        /// <returns>Returns a byte array representing the U16</returns>
        public override byte[] ToByteArray()
        {
            byte[] bytes = BitConverter.GetBytes(_Value);
            if (BitConverter.IsLittleEndian)
            {
                byte temp = bytes[1];
                bytes[1] = bytes[0];
                bytes[0] = temp;
            }
            return bytes;
        }
    }
}
